//
//  ST_MyFansViewController.swift
//  btvideo
//
//  Created by mac on 2020/5/25.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SwiftyJSON
import SwiftyUserDefaults
import MJRefresh

//MARK: - 我的粉丝
class ST_MyFansViewController: ST_BTLightBarViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
         
        
        title_nav_btn.setTitleForAllStates("我的粉丝")
        
        view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: statusBarHeight+44, left: 0, bottom: 0, right: 0))
        }
        
        request_data()
    }

    fileprivate var currentPage = 0
    
    
    
    
    fileprivate func request_data() {
        
        
        currentPage += 1
        
        JVrequest(api,parameters: ["page":currentPage,"size":21] ,success: { (json) in
            
            
//            print("22222----",json)
            
            let data = json["data"]
            
            self.total = data["total"].intValue
            
            var count = 0
            
            let list = data["list"].arrayValue.map { (itemJson) -> ST_MyFansCellModel in
                return ST_MyFansCellModel.init(fromJson: itemJson)
            }
            
            if self.currentPage == 1{
                self.data_array = list
            }else{
                self.data_array += list
            }
            
            count = list.count
            
            self.collectionView.reloadData()
            
            self.load_more_option_style(tableView: self.collectionView, count: count, pageSize: 21) {
                
                self.collectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingBlock: {[weak self] in

                    self?.request_data()
                })
            }
            
        }) { (code, err) in
            
            self.toast(text: err)
        }
        
        
    }
    
//    MARK: - override Properties
    
    var api = PH.fetchFansListApi
    
    var empty_image_name = "我的-粉丝-空状态"
    
    var empty_string = "发布特质才会获得更多粉丝哦~"
//    MARK: - Properties
    fileprivate var total = 0
    
    fileprivate lazy var layout:UICollectionViewFlowLayout = {
       let controlLayout = UICollectionViewFlowLayout()
        controlLayout.minimumLineSpacing = 15
        controlLayout.minimumInteritemSpacing = 5
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 10, bottom: BTSafeAreaBottom + 49 + 10, right: 10)
        
        controlLayout.itemSize = CGSize.init(width: ST_MyFansCell.width, height: ST_MyFansCell.height)
        controlLayout.headerReferenceSize = CGSize.init(width: ScreenWidth, height: 109)
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.delegate = self
        control.dataSource = self

        control.register(cellWithClass: ST_MyFansCell.self)
        
//        header
        control.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: ST_MyFansHeaderView.self)
        
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.backgroundColor = Color.white
        
        control.alwaysBounceVertical = true
        
        control.contentInset = UIEdgeInsets.init(top: statusBarHeight - 20, left: 0, bottom: 0, right: 0)
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        
        
        control.emptyDataSetSource = self
        control.emptyDataSetDelegate = self
        return control
    }()
    
    fileprivate var data_array = [ST_MyFansCellModel]()
    
    fileprivate var first = true
}


extension ST_MyFansViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    
    func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
        
        if first{
            self.first = false
            return false
        }else{
            return true
        }
    }
    
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let attrString = NSMutableAttributedString(string: empty_string)
        
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16),.foregroundColor: UIColor(red: 0.67, green: 0.71, blue: 0.75, alpha: 1)]
        
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        
        return attrString
    }
    
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        
        return UIImage.init(named: empty_image_name)
    }
    
}

extension ST_MyFansViewController:UICollectionViewDelegateFlowLayout,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_MyFansCell.self, for: indexPath)
        
        cell.child_model = data_array[indexPath.row]
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withClass: ST_MyFansHeaderView.self, for: indexPath)
        
        header.model = (self.total,self.api == PH.fetchFansListApi ? "粉丝" : "喜欢")
        
        return header
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        if data_array.count < 1{
            return CGSize.zero
        }else{
            return CGSize.init(width: ScreenWidth, height: 109)
        }
    }
    
}

//MARK: - 我的关注
class ST_MyFollowViewController: ST_MyFansViewController {
    
    
    override var api: String{
        get{
            return PH.fetchFollowListApi
        }
        set{
            super.api = newValue
        }
    }
    
    override var empty_image_name: String{
        get{
            return "我的-关注-空状态"
        }
        set{
            super.empty_image_name = newValue
        }
    }
    
    override var empty_string: String{
        get{
            return "特质广场可以发现超多特质呢~"
        }
        set{
            super.empty_string = newValue
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title_nav_btn.setTitleForAllStates("我的喜欢")
        
    }
}


//MARK: - Header

class ST_MyFansHeaderView: UICollectionReusableView {
        
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(15)
            make.height.equalTo(42)
        }
        
        
        self.addSubview(sub_title_label)
        sub_title_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(content_label.snp.bottom)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 30, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var sub_title_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
//        control.numberOfLines = 0
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    var model = (number:0,text:"粉丝"){
        didSet{
            content_label.text = model.number.string
            sub_title_label.text = model.text
        }
    }
    
}
